﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common.Entity;
using ESOL.Business.Fa;
using ESOL.Common;

namespace ESOL.WinUI.Fa
{
    public partial class frmManageAssetMaintenance : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private FaAssetsEntity mSelectedEntity;
        #endregion

        #region Construction
        public frmManageAssetMaintenance()
        {
            InitializeComponent();
        }
        #endregion

        #region Function
        private string GetCondition()
        {
            string condition = "";
            if (dtpLastMaintainDate.EditValue!=null)
            {
                condition += " AND M.LAST_MAINTAIN_DATE >= CONVERT(DATETIME,'" + dtpLastMaintainDate.EditValue + "')";
            }
            if (dtpLastMaintainDateTo.EditValue!=null)
            {
                condition += " AND M.LAST_MAINTAIN_DATE <= CONVERT(DATETIME,'" + dtpLastMaintainDateTo.EditValue+ "')";
            }
            if (txtAsset.Tag != null && txtAsset.Tag.GetType() == typeof(FaAssetsEntity))
            {
                FaAssetsEntity entity = (FaAssetsEntity)txtAsset.Tag;
                condition += " AND M.ASSET_ID = '" + entity.AssetId + "' ";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "A.ASSET_CODE";
            DataTable dt = FaManufacturerBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        #endregion


        #region Events
        private void frmManageAssetMaintenance_Load(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        private void txtAsset_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainAssetQuery frm = new frmMaintainAssetQuery();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK && frm.SelectedAsset != null)
            {
                txtAsset.Text = frm.SelectedAsset.AssetCode + " (" + frm.SelectedAsset.AssetName + ")";
                txtAsset.Tag = frm.SelectedAsset;
            }
            else
            {
                txtAsset.Text = "";
                txtAsset.Tag = null;
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedEntity = new FaAssetsEntity(dr);
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_View()
        {
            if (mSelectedEntity == null)
            {
                MessageBox.Show("请选择一个固定资产！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAssetMaintenance frm = new frmMaintainAssetMaintenance(mSelectedEntity);
            frm.ThisActionType = ActionType.View;
            frm.ShowDialog();
        }

        protected override void Click_New()
        {
            frmMaintainAssetMaintenance frm = new frmMaintainAssetMaintenance(mSelectedEntity);
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        private void btnHistory_Click(object sender, EventArgs e)
        {
            if (mSelectedEntity == null)
            {
                MessageBox.Show("请选择一个固定资产！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAssetMaintenanceHistory frm = new frmMaintainAssetMaintenanceHistory(mSelectedEntity);
            frm.ShowDialog();

        }

        #endregion


    }
}